<script>
import SelectBase from './SelectBase'
import jsonSysCode from '@/utils/store/jsonSysCode.js'
import { getSysCodeList } from '@/api/dictionary'
export default {
  mixins: [SelectBase],
  props: {
    dataType: {
      type: String,
      default: ''
    },
    isLocal: {
      type: Boolean,
      default: true
    },
    clearable: {
      type: Boolean,
      default: true
    },
    codeNameKey: {
      type: String,
      default: 'code_name'
    },
    codeValueKey: {
      type: String,
      default: 'code_value'
    }
  },
  created() {
    // if (!this.isLocal) {
    //   this.getCode()
    // } else {
    //   const data = jsonSysCode(this.dataType)
    //   this.data = data && this.formData(data)
    // }
    const data = jsonSysCode(this.dataType)
    this.data = data && this.formData(data)
  },
  methods: {
    getCode() {
      this.dataType && getSysCodeList(this.dataType).then(({ data }) => {
        data && (this.data = this.formData(data))
      }).catch(err => console.log(err))
    },
    formData(data) {
      return data.map(item => {
        return {
          value: item[this.codeValueKey],
          label: item[this.codeNameKey],
          ...item
        }
      })
    }
  }
}
</script>

